package com.book.controller;


import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.book.entity.CartEntity;
import com.book.entity.OrderEntity;
import com.book.service.OrderService;
import com.book.utils.PageUtils;
import com.book.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 
 *
 * @author yang
 * @email yang@gmail.com
 * @date 2020-12-05 15:21:51
 */

@RestController
@RequestMapping("/book/order")
public class OrderController {
    @Autowired
    private OrderService orderService;


    @GetMapping("/get/order/{userId}")
    public R getOrder(@PathVariable("userId")Long userId){
        List<OrderEntity> orderEntities =  orderService.getOrderById(userId);
        return R.ok().put("orders",orderEntities);
    }


    @PostMapping("/create/order/{userId}")
    public R createOrder(@PathVariable("userId")Long userId){
        String order = orderService.createOrder(userId);
        return R.ok().put("orderId",order);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody OrderEntity order){
		orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody OrderEntity order){
		orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
